package icu.shaoyayu.security.dome1.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author shaoyayu
 * @Version 1.0.0
 * @readme ：
 */
@Slf4j
public class MyAuthenticSuccessHandler implements AuthenticationSuccessHandler {

    private String url;

    public MyAuthenticSuccessHandler(String url){
        this.url = url;
    }

    /**
     * @param httpServletRequest
     * @param httpServletResponse
     * @param authentication
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        final User user = (User) authentication.getPrincipal();
        log.info(user.getUsername());
        log.info(user.getPassword());
        httpServletResponse.sendRedirect(url);
    }
}
